package com.centit.importData.controller;


import com.alibaba.fastjson.JSON;
import com.centit.airplanebaseinfo.service.AirplaneBaseinfoManager;
import com.centit.framework.common.JsonResultUtils;
import com.centit.importData.dao.JdndjhDao;
import com.centit.importData.dao.ShhzfjfdjDao;
import com.centit.importData.po.ExportExcelUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/export/importData")
public class ImportDataController {
    @Resource
    private ShhzfjfdjDao shhzfjfdjDao;
    @Resource
    private AirplaneBaseinfoManager airplaneBaseinfoManager;
    @Resource
    private JdndjhDao jdndjhDao;


    @RequestMapping(value = "/uploadShhzfjfdj", method = { RequestMethod.POST })
    //@ResponseBody
    //@MyDictionary
    public void uploadExcelShhzfjfdj(
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletResponse response, HttpServletRequest request) {
        String loginUser=request.getParameter("loginUser");
        try {
            String type = file.getOriginalFilename();
            System.out.println("--------type--------"+ type);
            /*导入方法首先校验模板是否对应*/
            ExportExcelUtil exportExcelUtil=new ExportExcelUtil();
            exportExcelUtil.initDao(shhzfjfdjDao,airplaneBaseinfoManager,jdndjhDao);
            /*初始化错误日志*/
            ExportExcelUtil.RESULT_STR_LIST_All_POOL_NP.put(loginUser,new ArrayList<>());
            String str=exportExcelUtil.importExcelShhzfj(file,loginUser,type);

            Map<String,Object> resultMap=new HashMap<>();
            resultMap.put("wrong.url","");
            resultMap.put("wrong.data",ExportExcelUtil.RESULT_STR_LIST_All_POOL_NP.get(loginUser));
            JsonResultUtils.writeSingleDataJson(resultMap, response);

        }catch (Exception e){
            e.printStackTrace();
            JsonResultUtils.writeErrorMessageJson(601, "操作失败,请联系系统管理员", response);
        }

    }

    @RequestMapping(value = "/uploadJdndjh", method = { RequestMethod.POST })
    public void uploadJdndjh(
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletResponse response, HttpServletRequest request) {
        String loginUser=request.getParameter("loginUser");
        try {
            String type = file.getOriginalFilename();
            /*导入方法首先校验模板是否对应*/
            ExportExcelUtil exportExcelUtil=new ExportExcelUtil();
            exportExcelUtil.initDao(shhzfjfdjDao,airplaneBaseinfoManager,jdndjhDao);
            /*初始化错误日志*/
            ExportExcelUtil.RESULT_STR_LIST_All_POOL_JDNDJH.put(loginUser,new ArrayList<>());
            String str=exportExcelUtil.importExcelJdndjh(file,loginUser,type);

            Map<String,Object> resultMap=new HashMap<>();
            resultMap.put("wrong.url","");
            resultMap.put("wrong.data",ExportExcelUtil.RESULT_STR_LIST_All_POOL_JDNDJH.get(loginUser));
            JsonResultUtils.writeSingleDataJson(resultMap, response);

        }catch (Exception e){
            e.printStackTrace();
            JsonResultUtils.writeErrorMessageJson(601, "操作失败,请联系系统管理员", response);
        }

    }

    @RequestMapping(value = "/uploadJdjbxx", method = { RequestMethod.POST })
    public void uploadJdjbxx(
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletResponse response, HttpServletRequest request) {
        String loginUser=request.getParameter("loginUser");
        try {
            String type = file.getOriginalFilename();
            /*导入方法首先校验模板是否对应*/
            ExportExcelUtil exportExcelUtil=new ExportExcelUtil();
            exportExcelUtil.initDao(shhzfjfdjDao,airplaneBaseinfoManager,jdndjhDao);
            /*初始化错误日志*/
            ExportExcelUtil.RESULT_STR_LIST_All_POOL_JDJBXX.put(loginUser,new ArrayList<>());
            String str=exportExcelUtil.importExcelJdjbxx(file,loginUser,type);

            Map<String,Object> resultMap=new HashMap<>();
            resultMap.put("wrong.url","");
            resultMap.put("wrong.data",ExportExcelUtil.RESULT_STR_LIST_All_POOL_JDJBXX.get(loginUser));
            JsonResultUtils.writeSingleDataJson(resultMap, response);

        }catch (Exception e){
            e.printStackTrace();
            JsonResultUtils.writeErrorMessageJson(601, "操作失败,请联系系统管理员", response);
        }

    }


}
